import { Injectable, Logger } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { ConfigService } from '@nestjs/config';
import { BaseService } from '@wwjCommon/base/base.service';
import { CacheService } from '@wwjCommon/cache/cache.service';
import { LoggingService } from '@wwjCommon/logging/logging.service';
import { UploadService } from '@wwjVendor/upload/upload.service';
import { PayService } from '@wwjVendor/pay/pay.service';
import { SmsService } from '@wwjVendor/sms/sms.service';
import { NoticeService } from '@wwjVendor/notice/notice.service';

@Injectable()
export class CoreWeappTemplateService extends BaseService<any> {
  private readonly logger = new Logger(CoreWeappTemplateService.name);

  constructor(
    @InjectRepository(Object)
    protected readonly repository: Repository<any>,
    private readonly cacheService: CacheService,
    private readonly configService: ConfigService,
    private readonly loggingService: LoggingService,
    private readonly uploadService: UploadService,
    private readonly payService: PayService,
    private readonly smsService: SmsService,
    private readonly noticeService: NoticeService,
  ) {
    super(repository);
  }

  /**
   * send
   * 对应 PHP: CoreWeappTemplateService_core::send()
   * 逻辑类型: undefined - undefined
   */
  async send(site_id: any[], template_id: any[], touser: any[], data: any[], page: any[]) {
    // 基于PHP真实逻辑: send
    // PHP原文: $api = CoreWeappService::appApiClient($site_id);         $api->postJson('cgi-bin/message/subscribe/send', [             'template_id' => $template_id,...
const api = CoreWeappService.appApiClient(site_id);
        api.postJson('cgi-bin/message/subscribe/send', [
            template_id: template_id, // 所需下发的订阅模板id
            touser: touser,     // 接收者（用户）的 openid
            page: page,       // 点击模板卡片后的跳转页面，仅限本小程序内的页面。支持带参数,（示例index?foo=bar）。该字段不填则模板无跳转。
            data: data,
        )];
    }

    /**
     * 组合模板并添加至帐号下的个人模板库
     * @param int site_id
     * @param tid
     * @param kidList
     * @param sceneDesc
     * @return array|Collection|object|ResponseInterface|string
     * @throws GuzzleBusinessException
     * @throws InvalidConfigBusinessException
     */
    async addTemplate(int site_id, tid, kidList, sceneDesc){
//        tid = 563;     // 模板标题 id，可通过接口获取，也可登录小程序后台查看获取
//        kidList = [1, 2];      // 开发者自行组合好的模板关键词列表，可以通过 `getTemplateKeywords` 方法获取
//        sceneDesc = '提示用户图书到期';    // 服务场景描述，非必填
        const api = CoreWeappService.appApiClient(site_id);
        return api.postJson('wxaapi/newtmpl/addtemplate', [
            tid: tid,
            kidList: kidList,
            sceneDesc: sceneDesc,
        )];
    }

    /**
     * 删除帐号下的个人模板
     * @param int site_id
     * @param template_id
     * @return array|Collection|object|ResponseInterface|string
     * @throws InvalidConfigBusinessException
     * @throws GuzzleBusinessException
     */
    async deleteTemplate(int site_id, template_id){
        const api = CoreWeappService.appApiClient(site_id);
        return api.postJson('wxaapi/newtmpl/deltemplate', [
            priTmplId: template_id,
        );
  }
  }

  /**
   * addTemplate
   * 对应 PHP: CoreWeappTemplateService_core::addTemplate()
   * 逻辑类型: undefined - undefined
   */
  async addTemplate(site_id: number, tid: number, kidList: number, sceneDesc: number) {
    // 基于PHP真实逻辑: addTemplate
    // PHP原文: //        $tid = 563;     // 模板标题 id，可通过接口获取，也可登录小程序后台查看获取 //        $kidList = [1, 2];      // 开发者自行组合好的模板关键词列表，可以通过 `getTemplateKeywords` 方法获取 //   ...
//        tid = 563;     // 模板标题 id，可通过接口获取，也可登录小程序后台查看获取
//        kidList = [1, 2];      // 开发者自行组合好的模板关键词列表，可以通过 `getTemplateKeywords` 方法获取
//        sceneDesc = '提示用户图书到期';    // 服务场景描述，非必填
        const api = CoreWeappService.appApiClient(site_id);
        return api.postJson('wxaapi/newtmpl/addtemplate', [
            tid: tid,
            kidList: kidList,
            sceneDesc: sceneDesc,
        )];
    }

    /**
     * 删除帐号下的个人模板
     * @param int site_id
     * @param template_id
     * @return array|Collection|object|ResponseInterface|string
     * @throws InvalidConfigBusinessException
     * @throws GuzzleBusinessException
     */
    async deleteTemplate(int site_id, template_id){
        const api = CoreWeappService.appApiClient(site_id);
        return api.postJson('wxaapi/newtmpl/deltemplate', [
            priTmplId: template_id,
        );
  }
  }

  /**
   * deleteTemplate
   * 对应 PHP: CoreWeappTemplateService_core::deleteTemplate()
   * 逻辑类型: undefined - undefined
   */
  async deleteTemplate(site_id: number, template_id: number) {
    // 基于PHP真实逻辑: deleteTemplate
    // PHP原文: $api = CoreWeappService::appApiClient($site_id);         return $api->postJson('wxaapi/newtmpl/deltemplate', [             'priTmplId' => $template_id...
const api = CoreWeappService.appApiClient(site_id);
        return api.postJson('wxaapi/newtmpl/deltemplate', [
            priTmplId: template_id,
        );
  }
  }
}
